#include "tetrixpiece.h"

TetrixPiece::TetrixPiece()
{
    _col_per_row = 0;
    _color = Qt::green;
    _array.clear();
}

TetrixPiece::TetrixPiece(int array[],int rows,int cols,QColor color)
{
    _color = color;

    if(rows > 0 && cols > 0)
    {
        _array.resize(rows * cols);
        _col_per_row = cols;

        for(int i = 0; i < rows; i++)
            for(int j = 0; j < cols; j++)
                _array.setBit(i * cols + j,array[i * cols + j] > 0);
    }
    else{
        _col_per_row = 0;
        _array.clear();
    }
}


bool TetrixPiece::isFilled(int row,int col) const
{
    if(row < height() && col < width())
        return _array.testBit(row * width() + col);

    return false;
}

bool TetrixPiece::isFilled(int index) const
{
    if(index < size())
        return _array.testBit(index);

    return false;
}

void TetrixPiece::rotateLeft()
{
    if(size() > 0)
    {
        int data[size()];        

        for(int row = 0; row < height(); row++)
        {
            for(int col = 0; col < width(); col++)
                data[(width() -1 - col) * height() + row] = isFilled(row * width() + col) ? 1 : 0;
        }

        for(int i = 0; i < size(); i++)
            _array.setBit(i,data[i]);

        _col_per_row = size()/_col_per_row;

    }
}

void TetrixPiece::clean()
{
    _col_per_row = 0;
    _array.clear();
}
